package com.mall.order.services;

import com.mall.commons.tool.exception.BizException;
import com.mall.commons.tool.exception.ProcessException;
import com.mall.order.OrderDeleteService;
import com.mall.order.dal.entitys.Order;
import com.mall.order.dal.entitys.OrderItem;
import com.mall.order.dal.entitys.OrderShipping;
import com.mall.order.dal.persistence.OrderItemMapper;
import com.mall.order.dal.persistence.OrderMapper;
import com.mall.order.dal.persistence.OrderShippingMapper;
import com.mall.order.dal.persistence.StockMapper;
import com.mall.order.dto.DeleteOrderRequest;
import com.mall.order.dto.DeleteOrderResponse;
import com.mall.order.utils.ExceptionProcessorUtils;
import com.mall.user.constants.SysRetCodeConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
public class OrderDeleteServiceImpl implements OrderDeleteService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderItemMapper orderItemMapper;
    @Autowired
    private OrderShippingMapper orderShippingMapper;
    @Autowired
    private StockMapper stockMapper;
    /**更新订单状态的方法，在订单状态更新时就已经做判断更新库存了。所以这里删除就不更新库存了
     *
     * 1 删除order表记录 2 删除order_item表关联记录 3 删除order_shipping关联记录
     * 根据删除的订单的状态判断是否需要返还库存 库存表stock;
     *
     * @param request
     * @return
     */
    @Transactional
    @Override
    public DeleteOrderResponse deleteOrder(DeleteOrderRequest request) {

        DeleteOrderResponse response = new DeleteOrderResponse();
        try {
            request.requestCheck();
            String orderId = request.getOrderId();

            //删除订单
            Order orderToDelete = new Order();
            orderToDelete.setOrderId(orderId);
            int deleteOrder = orderMapper.delete(orderToDelete);
            if (deleteOrder < 1) {
                throw new BizException("未定义删除失败code", "删除订单失败，回滚");
            }
            //删除订单条目
            OrderItem orderItemToDelete = new OrderItem();
            orderItemToDelete.setOrderId(orderId);
            int deleteOrderItem = orderItemMapper.delete(orderItemToDelete);
            if (deleteOrderItem < 1) {
                throw new BizException("未定义删除失败code", "删除订单条目失败，回滚");
            }
            //删除订单物流表关联条目
            OrderShipping orderShippingToDelete = new OrderShipping();
            orderShippingToDelete.setOrderId(orderId);
            int deleteOrderShipping = orderShippingMapper.delete(orderShippingToDelete);
            if (deleteOrderShipping != 1) {
                throw new BizException("未定义删除失败code", "删除订单物流失败，回滚");
            }
            response.setCode(SysRetCodeConstants.SUCCESS.getCode());
            response.setMsg(SysRetCodeConstants.SUCCESS.getMessage());
            return response;
        } catch (Exception e) {
            log.error("OrderDeleteServiceImpl.deleteOrder occurs exception : " + e);
            ExceptionProcessorUtils.wrapperHandlerException(response, e);
        }
        return response;
    }
}
